<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14
 * Time: 13:45
 */

namespace app\controllers;

use app\lib\BaseFunction;
use yii;
use app\models\Inquiry;
use app\models\InquiryType;
use app\models\Staff;
use app\models\CustomerRefer;

class InquiryController extends ApiBaseController
{
    public function actionList()
    {
        $p = is_numeric(yii::$app->request->get('p')) ? yii::$app->request->get('p') : 1;
        $s = is_numeric(yii::$app->request->get('s')) ? yii::$app->request->get('s') : 15;
        $where['name'] = trim(yii::$app->request->get('name'));
        $where['inquiry_detail'] = trim(yii::$app->request->get('inquiry_detail'));
        $where['email'] = trim(yii::$app->request->get('email'));
        $where['begin_time'] = yii::$app->request->get('begin_time');
        $where['end_time'] = yii::$app->request->get('end_time');
        $where['star'] = yii::$app->request->get('star');
        $where['recommend'] = yii::$app->request->get('recommend');
        $where['service_name'] = yii::$app->request->get('service_name');
        $where['status'] = yii::$app->request->get('status');
        $where['property'] = yii::$app->request->get('property');
        $where['refer'] = yii::$app->request->get('refer');
        if ($this->user['role'] != 1) {
            $where['service_name'] = $this->user['job_number'];
        }
        if ($this->user['job_number'] == 7) {
            $where['service_name'] = [7, 22];
        }
        $model = new Inquiry();
        $size = (int)$model->get_inquiry_size($where);
        $list = $model->get_inquiry_list($p, $s, $where);
        return \json_encode(['list' => $list, 'total' => $size]);

    }

    public function actionCommon()
    {
        $staff = new Staff();
        $staff_list = $staff->get_staff_list();
        $model = new InquiryType();
        $inquiryType = $model->list();
        $model = new CustomerRefer();
        $customerRefer = $model->list();
        $data = [];
        $data['staff'] = $staff_list;
        $data['type'] = $inquiryType;
        $data['refer'] = $customerRefer;
        return \json_encode($data);
    }


    public function actionAdd()
    {
        $data = yii::$app->request->post('data');
        $data = \json_decode($data, true);
        $model = new Inquiry();
        $re = $model->insert($data);
        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '添加失败,请重试']);
        }
    }

    /**删除询盘
     * @return string
     */
    public function actionDel()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'method not allowed']);
        }
        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }
        BaseFunction::check_edit_auth($this->user['role']);
        $model = new Inquiry();
        $re = $model->delete(['id' => $id]);
        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '删除成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
        }
    }

    /**修改询盘
     * @return string
     */
    public function actionUpdate()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'method not allowed']);
        }
        $id = yii::$app->request->post('id');
        $value = yii::$app->request->post('v');
        $column = yii::$app->request->post('column');
        if (!is_numeric($id) || $id < 0 || !$column) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }

        $model = new Inquiry();
        $re = $model->save([$column => $value], ['id' => $id]);
        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '更新成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '更新失败,请重试']);
        }
    }


    public function actionWebsiteInquiry()
    {
        $p = yii::$app->request->get('p', 1);
        $s = yii::$app->request->get('s', 10);
        $where = [];
        if ($this->user['role'] != 1 && $this->user['role'] != 2) {
            $where['follow_man'] = (int)$this->user['job_number'];
        }
        $db = new yii\db\Query();
        $db->select('a.*,b.customer_name,b.number,b.email')->from('jingsourcing_new_inquiry as a')
            ->leftJoin('jingsourcing_customer as b', 'a.user_id=b.id');

        if (!empty($where['follow_man'])) {
            $db->andWhere(['!=', 'b.follow_man', '']);
            $db->andWhere("JSON_CONTAINS(b.follow_man,'\"{$where['follow_man']}\"')");
        }

        $list = $db->orderBy(['a.time' => SORT_DESC])->offset(($p - 1) * $s)->limit($s)->all();

        $db = new yii\db\Query();
        $re = $db->select('count(*) as num')->from('jingsourcing_new_inquiry')->one();
        $total = $re ? $re['num'] : 0;
        return \json_encode(['list' => $list, 'total' => (int)$total]);
    }

}